package com.irheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;


public class BaseServlet extends HttpServlet{

//    一个Servlet完成多个功能   反射处理传入的相应方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();  //获取标识符路径
        String substring  = requestURI.substring(requestURI.lastIndexOf("/") + 1);//取标识符最后的目录
        try {
            Method method = this.getClass().getDeclaredMethod(substring, HttpServletRequest.class, HttpServletResponse.class);
           //当前方法的对象字节码
            method.setAccessible(true);
            method.invoke(this,req,resp);

        } catch (Exception e) {

        }
    }
}
